package com.ruoyi.car.service.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Objects;

import com.ruoyi.car.domain.CarGarage;
import com.ruoyi.car.domain.CarInfo;
import com.ruoyi.car.service.ICarGarageService;
import com.ruoyi.car.service.ICarInfoService;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.car.mapper.CarParkRecordMapper;
import com.ruoyi.car.domain.CarParkRecord;
import com.ruoyi.car.service.ICarParkRecordService;

/**
 * 停车情况Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-03
 */
@Service
public class CarParkRecordServiceImpl implements ICarParkRecordService 
{
    @Autowired
    private CarParkRecordMapper carParkRecordMapper;

    @Autowired
    private ICarInfoService carInfoService;

    @Autowired
    private ICarGarageService carGarageService;
    /**
     * 查询停车情况
     * 
     * @param parkRecordId 停车情况主键
     * @return 停车情况
     */
    @Override
    public CarParkRecord selectCarParkRecordByParkRecordId(Long parkRecordId)
    {

        CarParkRecord vo=carParkRecordMapper.selectCarParkRecordByParkRecordId(parkRecordId);
                vo.setGarageName(carGarageService.selectCarGarageByGarageId(vo.getGarageId()).getGarageName());
        return vo;
    }

    /**
     * 查询停车情况列表
     * 
     * @param carParkRecord 停车情况
     * @return 停车情况
     */
    @Override
    public List<CarParkRecord> selectCarParkRecordList(CarParkRecord carParkRecord)
    {
        List<CarParkRecord> list=carParkRecordMapper.selectCarParkRecordList(carParkRecord);
        CarParkRecord vo=null;
        Iterator<CarParkRecord> it=list.iterator();
        while (it.hasNext()){
            vo=it.next();
            vo.setGarageName(carGarageService.selectCarGarageByGarageId(vo.getGarageId()).getGarageName());

        }

        return list;
    }

    /**
     * 新增停车情况
     * 
     * @param carParkRecord 停车情况
     * @return 结果
     */
    @Override
    public int insertCarParkRecord(CarParkRecord carParkRecord)
    {
        //验证车牌
      carInfoService.compare(carParkRecord.getCarNumber());

      //修改车库车位数量
        carGarageService.editCarGarageNumber(carParkRecord.getGarageId());

        //修改汽车车库信息
        CarInfo carInfo=new CarInfo();
        carInfo.setCarNumber(carParkRecord.getCarNumber());
        carInfo.setColour(String.valueOf(carParkRecord.getGarageId()));
        carInfoService.updateCarInfoByCarNumber(carInfo);


        carParkRecord.setCreateTime(DateUtils.getNowDate());
        return carParkRecordMapper.insertCarParkRecord(carParkRecord);
    }

    /**
     * 修改停车情况
     * 
     * @param carParkRecord 停车情况
     * @return 结果
     */
    @Override
    public int updateCarParkRecord(CarParkRecord carParkRecord)
    {

        //验证车牌
        carInfoService.compare(carParkRecord.getCarNumber());

        if (ObjectUtils.isEmpty(carParkRecordMapper.selectCarParkRecordByParkRecordId(carParkRecord.getParkRecordId()).getParkEndTime())){
        if (ObjectUtils.isNotEmpty(carParkRecord.getParkEndTime())) {
            //修改车库车位数量
            carGarageService.reduceCarGarageNumber(carParkRecord.getGarageId());

            //修改汽车车库信息
            CarInfo carInfo = new CarInfo();
            carInfo.setCarNumber(carParkRecord.getCarNumber());
            carInfo.setColour("0");
            carInfoService.updateCarInfoByCarNumber(carInfo);

        }}
        carParkRecord.setUpdateTime(DateUtils.getNowDate());
        return carParkRecordMapper.updateCarParkRecord(carParkRecord);
    }

    /**
     * 批量删除停车情况
     * 
     * @param parkRecordIds 需要删除的停车情况主键
     * @return 结果
     */
    @Override
    public int deleteCarParkRecordByParkRecordIds(Long[] parkRecordIds)
    {
        return carParkRecordMapper.deleteCarParkRecordByParkRecordIds(parkRecordIds);
    }

    /**
     * 删除停车情况信息
     * 
     * @param parkRecordId 停车情况主键
     * @return 结果
     */
    @Override
    public int deleteCarParkRecordByParkRecordId(Long parkRecordId)
    {
        return carParkRecordMapper.deleteCarParkRecordByParkRecordId(parkRecordId);
    }
}
